import { Component, ChangeDetectionStrategy, Input, NgZone, ChangeDetectorRef } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd';

/**
 * 维修原因合计饼图
 */
@Component({
  selector: 'repair-reason-pie',
  templateUrl: './repair-reason-pie.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: {
    '[class.card]': 'true',
  },
})
export class RepairReasonPieComponent {
  @Input()
  title = '';
  _codeName = null;
  @Input()
  set codeName(val) {
    if (val) {
      this._codeName = val;
    }
  }
  @Input()
  set raw(val) {
    if (val && val.sum) {
      this._data = val.sum;
      this.attachData();
    }
  }

  total = 0;
  _data = [];
  time = null;
  data = [];

  constructor(
    public http: _HttpClient,
    public msg: NzMessageService,
    private ngZone: NgZone,
    private cdr: ChangeDetectorRef,
  ) {}

  private attachData() {
    const { _data } = this;
    this.data = [];
    this.time = new Date();
    this.total = 0;
    // tslint:disable-next-line: forin
    for (const key in _data) {
      this.total += +_data[key];
      this.data.push({ x: key, y: +_data[key] });
    }

    this.cdr.detectChanges();
  }

  format(val: number) {
    return `${val}`;
  }

  fmtColor(PROBLEM_TYPE) {
    if ('人为问题' === PROBLEM_TYPE) {
      return '#2194ff';
    } else if ('其他问题' === PROBLEM_TYPE) {
      return '#face1d';
    } else if ('质量问题' === PROBLEM_TYPE) {
      return '#f50';
    }
  }
}
